<script setup lang="ts">
import CustomNavbar from './components/CustomNavbar.vue'
import { getHomeBannerAPI } from '@/services/home'
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import type { BannerItem } from '@/types/home'
import CategoryPanel from './components/CategoryPanel.vue'
import type { CategoryItem } from '@/types/home'
import { getHomeCategoryAPI } from '@/services/home'
import type { HotItem } from '@/types/home'
import { getHomeHotAPI } from '@/services/home'
import HomePannel from './components/HomePannel.vue'
import PageSkeleton from './components/pageSkeleon.vue'
import { useGuessLike } from '@/composables'

//获取轮播图数据
const bannerList = ref<BannerItem[]>([])
const getHomeBannerList = async () => {
  const res = await getHomeBannerAPI()
  console.log(res)
  bannerList.value = res.result
}

//获取前台分类
const CategoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  CategoryList.value = res.result
}

// 获取热门推荐
const hotList = ref<HotItem[]>([])
const getHomeHotData = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}
const { guessRef, onScrolltolower } = useGuessLike()

const isLoading = ref(false)
// 页面加载
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerList(), getHomeCategoryData(), getHomeHotData()])
  isLoading.value = false
})

//下拉刷新
const isTriggered = ref(false)
const onRefresh = async () => {
  //开启动画
  isTriggered.value = true
  // await getHomeBannerList(),
  // await getHomeCategoryData(),
  // await getHomeHotData()
  guessRef.value?.resetData()
  await Promise.all([
    getHomeBannerList(),
    getHomeCategoryData(),
    getHomeHotData(),
    guessRef.value?.getMore(),
  ])
  //关闭动画
  isTriggered.value = false
}
</script>

<template>
  <CustomNavbar></CustomNavbar>
  <scroll-view
    scroll-y
    class="scroll-view"
    @scrolltolower="onScrolltolower"
    :refresher-triggered="isTriggered"
    refresher-enabled
    @refresherrefresh="onRefresh"
  >
    <PageSkeleton v-if="isLoading"> </PageSkeleton>
    <template v-else>
      <XtxSwiper :list="bannerList"></XtxSwiper>
      <CategoryPanel :list="CategoryList"></CategoryPanel>
      <HomePannel :list="hotList"></HomePannel>
      <XtxGuess ref="guessRef"></XtxGuess>
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.scroll-view {
  flex: 1;
}
</style>
